using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace MKDS_Course_Editor.MKDSEditor
{
    public partial class CourseSelector : Form
    {
        string file;
        NSMBe4.DSFileSystem.Arm9BinaryHandler arm;
        NSMBe4.DSFileSystem.NitroROMFilesystem n;
        NSMBe4.DSFileSystem.Directory Course;
        public CourseSelector(string file)
        {
            InitializeComponent();
            this.file = file;
        }

        private void CourseSelector_Load(object sender, EventArgs e)
        {
            n = new NSMBe4.DSFileSystem.NitroROMFilesystem(file);
            //arm = new NSMBe4.DSFileSystem.Arm9BinaryHandler(n);
            //arm.decompress();
            //arm.loadSections();
            Course = n.getDirByPath(@"root/data/Course");
            for (int i = 0; i < Course.childrenFiles.Count; i++)
            {
                if (!Course.childrenFiles[i].name.EndsWith("Tex.carc") && !Course.childrenFiles[i].name.EndsWith("D.carc"))
                {
                    ListViewItem it = new ListViewItem(Course.childrenFiles[i].name.Replace(".carc", ""));
                    it.Tag = i;
                    listView1.Items.Add(it);
                }
            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        string path = "";
        string path2 = "";
        string path3 = "";
        private void listView1_ItemActivate(object sender, EventArgs e)
        {
            NSMBe4.DSFileSystem.File it = Course.childrenFiles[(int)listView1.SelectedItems[0].Tag];
            int nr = 1;
            while (Course.childrenFiles[(int)listView1.SelectedItems[0].Tag + nr].name.EndsWith("D.carc") || Course.childrenFiles[(int)listView1.SelectedItems[0].Tag + nr].name.EndsWith("DTex.carc")) nr++;
            NSMBe4.DSFileSystem.File it2 = Course.childrenFiles[(int)listView1.SelectedItems[0].Tag+nr];
            NSMBe4.DSFileSystem.File it3 = n.getDirByPath(@"root/data/Main").childrenFiles[0];
            byte[] carc = it.getContents();
            path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\MKDS temp\\" + Guid.NewGuid().ToString();
            System.IO.Directory.CreateDirectory(path);
            File.WriteAllBytes(path + "\\" + it.name, carc);
            File.WriteAllBytes(path + "\\" + it.name.Replace("carc", "NARC"), NSMBe4.ROM.LZ77_Decompress(File.ReadAllBytes(path + "\\" + it.name)));
            System.IO.Directory.CreateDirectory(path + "\\" + "unpack");
            narc.unpack(path + "\\" + it.name.Replace("carc", "NARC"), path + "\\" + "unpack");

            byte[] carc2 = it2.getContents();
            path2 = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\MKDS temp\\" + Guid.NewGuid().ToString();
            System.IO.Directory.CreateDirectory(path2);
            File.WriteAllBytes(path2 + "\\" + it2.name, carc2);
            File.WriteAllBytes(path2 + "\\" + it2.name.Replace("carc", "NARC"), NSMBe4.ROM.LZ77_Decompress(File.ReadAllBytes(path2 + "\\" + it2.name)));
            System.IO.Directory.CreateDirectory(path2 + "\\" + "unpack");
            narc.unpack(path2 + "\\" + it2.name.Replace("carc", "NARC"), path2 + "\\" + "unpack");

            byte[] carc3 = it3.getContents();
            path3 = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\MKDS temp\\" + Guid.NewGuid().ToString();
            System.IO.Directory.CreateDirectory(path3);
            File.WriteAllBytes(path3 + "\\" + it3.name, carc3);
            File.WriteAllBytes(path3 + "\\" + it3.name.Replace("carc", "NARC"), NSMBe4.ROM.LZ77_Decompress(File.ReadAllBytes(path3 + "\\" + it3.name)));
            System.IO.Directory.CreateDirectory(path3 + "\\" + "unpack");
            narc.unpack(path3 + "\\" + it3.name.Replace("carc", "NARC"), path3 + "\\" + "unpack");

            CourseEditor ed = new CourseEditor(path,path2,path3, listView1.SelectedIndices[0]);
            ed.ShowDialog();
        }
    }
}
